package com.bw.ssm.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.bw.ssm.pojo.JUser;
import com.bw.ssm.service.JUserService;
import com.bw.ssm.util.FilesUtils;
import com.bw.ssm.util.FreeMarkerUtil;

import freemarker.template.Template;
import freemarker.template.TemplateException;

@Controller
@RequestMapping("/user")
public class JUserController {
	
	@Autowired
	private JUserService service;

	@RequestMapping("/list")
	public String list(Model model,JUser juser){
		List<JUser> l = service.list(juser);
		model.addAttribute("userList", l);
		return "bootstrap/list";
	}
	@RequestMapping("/toAdd")
	public String toAdd(Model model){
		return "bootstrap/add";
	}
	@RequestMapping("/add")
	public String add(HttpServletRequest request,JUser juser,MultipartFile file){

		String imgPath = FilesUtils.FilesUpload_xm(request, file, "//upload");
		juser.setImg(imgPath);
		int r = service.add(juser);
		return "redirect: list";
	}
	@RequestMapping("/toUpdate")
	public String toUpdate(Model model, JUser juser){
		juser = service.getById(juser.getId());
		model.addAttribute("juser", juser);
		return "update";
	}
	@RequestMapping("/update")
	public String update(Model model ,JUser juser){
		int r = service.update(juser);
		
		return "redirect: list";
	}
	@RequestMapping("/getByID")
	public String getByID(Model model, JUser juser){

		juser = service.getById(juser.getId());
		model.addAttribute("juser", juser);
		return "detail";
	}
	@RequestMapping("/createDetail")
	public String createDetail(HttpServletRequest request,Model model, JUser juser) throws IOException, TemplateException{

		juser = service.getById(juser.getId());
		model.addAttribute("juser", juser);
		
//		String path = "src/main/webapp/html/";
//		String path = System.getProperty("user.dir");
		String serverPath = request.getSession().getServletContext().getRealPath("");
		String detailFileName = "/html/"+"user"+juser.getId()+".html";
		System.out.println("html文件根目录："+new File(serverPath+detailFileName).getAbsolutePath());
		Writer w = new FileWriter(serverPath+detailFileName);
		Template template = FreeMarkerUtil.getTemplate(serverPath,"detail.ftl");
		

		String appPath = request.getContextPath();
		System.out.println("项目名："+appPath);
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("juser", juser);
		root.put("path", appPath);
		
		template.process(root, w);
		
		juser.setUrl(detailFileName);
		service.update(juser);
		
		return "redirect: list";
	}
	@RequestMapping("/delete")
	public String delete(Model model, JUser juser){
		int r = service.delete(juser);
		
		return "redirect: list";
	}
	@RequestMapping("/login")
	public String toLogin(JUser juser){
		
		//qu shuj 
		JUser juserForDb = service.getById(juser.getId());
		//panduan 
//		if()
		
		return "redirect: list";
	}
}
